# -*- coding: utf-8 -*-
# created on 2016/6/27

from sympy import S, solveset, EmptySet
from mathsolver.functions.base import BaseFunction, BaseEq, BasePoint, BaseEqs, new_latex


# 点与点关于坐标轴对称问题
class HanShuPointDuiCheng(BaseFunction):
    def solver(self, *args):
        point1 = args[0].sympify()
        point2 = args[1].sympify()
        axis = args[2].value
        eqs = []
        if "x轴" == axis:
            eqs.append([point1[0], point2[0]])
            eqs.append([point1[1], -point2[1]])
        else:
            eqs.append([point1[0], -point2[0]])
            eqs.append([point1[1], point2[1]])

        eqs = [[f1, f2] for f1, f2 in eqs if len((f1 - f2).free_symbols) > 0]
        if len(eqs) == 1:
            self.output.append(BaseEq(eqs[0]))
        elif len(eqs) > 1:
            self.output.append(BaseEqs(eqs))
        else:
            raise ValueError
        self.steps.append(["因为%s与%s关于%s对称" % (args[0].printing(), args[1].printing(), axis), self.output_eqs(eqs)])
        self.label.add("点与点关于坐标轴对称问题")
        return self


# 判断点所在的象限
class HanShuPointXiangXian(BaseFunction):
    @staticmethod
    def compare2zero(expr):
        if len(expr.free_symbols) == 0:
            if expr > 0:
                return ">"
            elif expr < 0:
                return "<"
            else:
                raise ValueError
        else:
            sym = expr.free_symbols.pop()
            ans = solveset(expr, sym, S.Reals)
            assert isinstance(ans, EmptySet)
            value = expr.subs({sym: 1})
            if value > 0:
                return ">"
            elif value < 0:
                return "<"
            else:
                return ValueError

    def solver(self, *args):
        p1, p2 = args[0].sympify()
        if len(args) > 1:
            symbol_value = args[1].value
            pair = dict([(key, item[0]) for key, item in symbol_value.items()])
            p1 = p1.subs(pair)
            p2 = p2.subs(pair)
            self.steps.append(["带入%s" % args[0].printing, "得到点的横坐标%s，纵坐标%s" % (new_latex(p1), new_latex(p2))])

        rel1 = self.compare2zero(p1)
        rel2 = self.compare2zero(p2)

        xiangxian = {(">", ">"): "一", ("<", ">"): "二", ("<", "<"): "三", (">", "<"): "四"}

        self.steps.append(["因为横坐标%s，纵坐标%s" % (self.output_eq([p1, rel1, 0]), self.output_eq([p2, rel2, 0])),
                           "点%s在第%s象限" % (args[0].printing(), xiangxian[(rel1, rel2)])])
        self.label.add("判断点所在的象限")
        return self


# 点在坐标轴上求参
class HanShuPointOnAxis(BaseFunction):
    def solver(self, *args):
        p1, p2 = args[0].sympify()
        axis = args[1].value
        if "x轴" == axis:
            eq = [p2, S.Zero]
            self.label.add("点在x轴上求参")
        else:
            eq = [p1, S.Zero]
            self.label.add("点在y轴上求参")
        self.output.append(BaseEq(eq))
        self.steps.append(["因为%s在%s上" % (args[0].printing(), axis), self.output_eq(eq)])
        return self


# 求点关于坐标轴的对称点坐标
class HanShuPointAxisDuiChen(BaseFunction):
    def solver(self, *args):
        p1, p2 = args[0].sympify()
        axis = args[1].value

        if "x轴" == axis:
            t1 = p1
            t2 = -p2
            self.label.add("求点关于x轴的对称点坐标")
        else:
            t1 = -p1
            t2 = p2
            self.label.add("求点关于y轴的对称点坐标")

        point = BasePoint([t1, t2])
        self.steps.append(["", "对称点的坐标是%s" % point.printing()])
        self.output.append(point)

        return self


if __name__ == '__main__':
    pass
